from typing import Dict, Union
import numpy as np
import pandas as pd


class DiffRecord:
    """差异记录基类"""

    def to_dict(self) -> Dict:
        """将差异记录转换为字典格式"""
        raise NotImplementedError("子类必须实现此方法")


class SheetAdded(DiffRecord):
    """新增工作表记录"""

    def __init__(self, sheet_name: str):
        self.sheet_name = sheet_name

    def to_dict(self) -> Dict:
        return {
            "差异类型": "Sheet新增",
            "Sheet名称": self.sheet_name,
            "行号": None,
            "列号": None,
            "旧值": None,
            "新值": None,
        }


class SheetDeleted(DiffRecord):
    """删除工作表记录"""

    def __init__(self, sheet_name: str):
        self.sheet_name = sheet_name

    def to_dict(self) -> Dict:
        return {
            "差异类型": "Sheet删除",
            "Sheet名称": self.sheet_name,
            "行号": None,
            "列号": None,
            "旧值": None,
            "新值": None,
        }


class CellDiff(DiffRecord):
    """单元格差异记录"""

    DIFF_TYPES = ["新增单元格", "删除单元格", "值不同"]

    def __init__(
        self,
        sheet_name: str,
        row: int,
        col: int,
        old_val: object,
        new_val: object,
        diff_type: str,
    ):
        if diff_type not in self.DIFF_TYPES:
            raise ValueError(f"无效的差异类型，必须是: {self.DIFF_TYPES}")

        self.sheet_name = sheet_name
        self.row = row
        self.col = col
        self.old_val = old_val
        self.new_val = new_val
        self.diff_type = diff_type

    def to_dict(self) -> Dict:
        return {
            "差异类型": self.diff_type,
            "Sheet名称": self.sheet_name,
            "行号": self.row + 1,
            "列号": self.col + 1,
            "旧值": self.old_val,
            "新值": self.new_val,
        }
